import $ from '../common';
Page({
  data: {
    week: ['一', '二', '三', '四', '五', '六', '日'],
    arr: [{}, {}, {}],
    currMonthIndex: 0, //swiper展示的第几个
    clickinfo: '', //记录点击的时间，然后通过每一项时间判断点击的哪一个是那一个，然后添加样式  排他
    isfrist: false, //一开始默认选择的时候今日的日期，点击不存在，如果选择其他时间，点击就会存在，如果点击对应的时间第点击将它变成不存在，然后更改默认选择的样式
    lessinfo: [],
    count: 0,
    fristload: true,
    titleHeight: $.gData().titleHeight,
    statuHeight: $.gData().statuHeight,
    // istip:true,
    ner:'',
    leave_type:'',
    _components_modal_hide:true,
    remark:''
  },
  onLoad(e) {
    this.getday()
  },
  close(){//关闭模态框
    this.setData({ _components_modal_hide: true });
  },
  showPopup() {//打开模态框
    this.setData({ _components_modal_hide: false });
  },
  onShow() {
    // this.getday()
  },
  getday(){
    let date = new Date() //获取时间
    let year = date.getFullYear() //获取年
    let month = date.getMonth() //获取月

    let a = month + 1
    let months = a > 10 ? a : '0' + a
    let day = date.getDate()
    let toa=day<10?'0'+day:day
    let today = year + '-' + months + '-' + toa //今天的时间 格式是2022-04-20
    this.setData({
      years: year,
      months: a,
      today,
    })
    this.getinfo(year, month + 1) //本月
    this.getinfo(year, month + 2, 1) //下月
    this.getinfo(year, month, 2) //上月
  },
  itemdayclick(e) {//每一项的点击
    let {
      today,
      isfrist,
      lessinfo
    } = this.data
    if (e.currentTarget.dataset.clickinfo != today) {
      isfrist = true
    } else {
      isfrist = false
    }
    this.setData({
      clickinfo: e.currentTarget.dataset.clickinfo,
      lessinfo: e.currentTarget.dataset.clicklesson,
      isfrist,
    })
  },
  getinfo(year, month, index = 0) { //获取日期
    let {  months,  years, today,lessinfo} = this.data
    if (month > 12) {
      year++
      let time = month - 12
      month = time
    } else if (month < 1) {
      if (month == 0) {
        month = 12
      } else if (month == -1) {
        month = 11
      }
      year--;
    }
    this.data.count++
    $.get('/user/calendar', {
      year,
      month,
      status: ''
    }, res => {
      this.data.count--
      res.calendar.forEach(v => {
        v.forEach(a => {
          if (a.lessonstudents.length > 0) {
            a.current_month == "NO" ? a.color = '#e9cac6' : a.color = '#c5666a'
          } else {
            a.current_month == "NO" ? a.color = '#e0e0e0' : a.color = '#9e9e9e'
          }
          if (months == month && years == year && this.data.fristload) {//初次渲染
            if (today == a.dateseq) {
              lessinfo = a.lessonstudents||[]
              this.setData({
                lessinfo,
                fristload: false
              })
            }
          }
          if(this.data.clickinfo && this.data.clickinfo==a.dateseq){
            lessinfo = a.lessonstudents||[]
            this.setData({
              lessinfo,
            })
          }
          a.itemday = a.dateseq.split('-')[2]
        })
      });
      this.data.arr[index].year = year
      this.data.arr[index].month = month
      this.data.arr[index].days = res
      this.setData({
        arr: this.data.arr,
      })
      if(this.data.count==0){
        wx.hideLoading()
      }
    })
  },
  leftclick() { //左边点击
    const {
      currMonthIndex
    } = this.data
    let a = currMonthIndex
    a === 0 ? a = 2 : a--
    this.setData({
      currMonthIndex: a
    })
  },
  rightclick() { //右边点击
    const {
      currMonthIndex
    } = this.data
    let a = currMonthIndex
    a === 2 ? a = 0 : a++
    this.setData({
      currMonthIndex: a
    })
  },
  calendarSwiper(e) { //swiper滑动触发
    let currMonthIndex = e.detail.current
    let thisMonth = this.data.arr[currMonthIndex]
    let {
      month,
      year
    } = thisMonth
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    if (currMonthIndex === 0) {
      this.getinfo(year, month + 1, 1)
      this.getinfo(year, month - 1, 2)
    } else if (currMonthIndex === 1) {
      this.getinfo(year, month + 1, 2)
      this.getinfo(year, month - 1, 0)
    } else if (currMonthIndex === 2) {
      this.getinfo(year, month + 1, 0)
      this.getinfo(year, month - 1, 1)
    }
    this.setData({
      currMonthIndex
    })
  },
  onReachBottom() {

  },
  textinput(e){
    this.setData({
      remark:e.detail.value
    })
  },
  cancel(e){//取消请假
    const{student_uuid,status='',lesson_uuid}=e.currentTarget.dataset
    this.setData({ student_uuid,lesson_uuid})
    if(status){
      $.post('/student/leave/canel',{      
        lesson_uuid,
        student_uuid
      },res=>{
        let click=this.data.clickinfo.split('-')
        this.getinfo(click[0], click[1], this.data.currMonthIndex)
      },reject=>{
        this.setData({
          istip: true,
          ner: reject.message
        })
        setTimeout(() => {
          this.setData({
            istip: false,
          })
        }, 2000);
      })
    }else{
      this.showPopup()
    }
  },
  radioChange(e){
    this.setData({
      leave_type:e.detail.value
    })
  },
  sumbit(){
    if(!this.data.remark){
      this.setData({
        istip: true,
        ner: '请输入备注内容'
      })
      setTimeout(() => {
        this.setData({
          istip: false,
        })
      }, 2000)
      return
    }
    if(!this.data.leave_type){
      this.setData({
        istip: true,
        ner: '请选择学员类型'
      })
      setTimeout(() => {
        this.setData({
          istip: false,
        })
      }, 2000)
      return
    }
    $.post('/student/leave',{
      lesson_uuid:this.data.lesson_uuid,
      student_uuid:this.data.student_uuid,
      remark:this.data.remark||'',
      leave_type:this.data.leave_type||''
    },res=>{
      let click=this.data.clickinfo.split('-')
      this.setData({ _components_modal_hide: true,remark:'',leave_type:'' });
      this.getinfo(click[0], click[1], this.data.currMonthIndex)
    })
  }
})